package com.liu.interceptor;

import com.fasterxml.jackson.core.type.TypeReference;
import com.liu.configuration.CustomObjectMapper;
import com.liu.constant.Constants;
import com.liu.core.RedisTemplate;
import com.liu.entity.LiuLoginUser;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName LoginInterceptor
 * @Author liuxin
 * @Date Created in 2023-01-20 22:57
 * @Version 1.0
 */

public class LoginInterceptor implements HandlerInterceptor {
    @Resource
    RedisTemplate redisTemplate;

    @Resource
    CustomObjectMapper customObjectMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1设置一个白名单,这里直接添加至配置文件使用
//        List<String> whiteNames = new ArrayList<String>();
//        whiteNames.add("/admin/login");
//        if(whiteNames.contains(request.getRequestURI())) {
//            return true;
//        }

        if (Constants.DEBUG_INTERCEPTOR) {
            System.out.println("进入了过滤器");
            ResponseEntity<String> res = ResponseEntity.status(401).body("bad Credentials!");

            //2.如果不是白名单，检测
            //判断有没有Authorization这个请求头，拿到首部信息的Authorization的值，也就是token
            String token = request.getHeader(Constants.TOKEN_AUTHORIZATION);
            if (token == null) {
                response.setStatus(401);
                response.getWriter().write(customObjectMapper.writeValueAsString(res));

                return false;
            }
            System.out.println("获取了token");

            System.out.println(request);
            //通过前端获取username
//        String userName = request.getHeader("userName");

            //通过redis的keys获取username
            Set<String> keys = redisTemplate.keys(Constants.TOKEN_PREFIX + "*" + token);
            if (keys == null || keys.size() == 0) {
                response.setStatus(403);
                response.getWriter().write(customObjectMapper.writeValueAsString(res));

                return false;
            }
            String tokenKey = (String) keys.toArray()[0];

            //3.使用token取redis中查看，有没有对应的loginUser
            LiuLoginUser liuLoginUser = redisTemplate.getObject(tokenKey, new TypeReference<LiuLoginUser>() {
            });
            if (liuLoginUser == null) {
                response.setStatus(405);
                response.getWriter().write(customObjectMapper.writeValueAsString(res));
                return false;
            }

            System.out.println("获取了liuLogUser");
            //只要不为空且放行了，就延长token时间,续命
            redisTemplate.expire(Constants.TOKEN_PREFIX + token, Constants.TOKEN_TIME);

            System.out.println("成功延长");
            return true;
        } else {
            //这里是关闭过滤器直接放行
            return true;
        }
//        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
